'''聚合权限接口'''


from ..postgresql_connection import PostgreSQLConnection

from .permissions_table import PermissionsTable

from .role_permissions_table import RolePermissionsTable
from .user_roles_table import UserRolesTable

from . import logger
class PermissionAggregationInterface:


    def __init__(self, connection, cursor, user_id, permission_path):

        self.user_id = user_id
        self.permission_path = permission_path
        self.connection = connection
        self.cursor = cursor

        
    @property
    def is_permission_allowed(self):
        '''判断用户是否有权限访问'''

        permissions_table = PermissionsTable(self.connection, self.cursor)

        role_permissions_table = RolePermissionsTable(self.connection, self.cursor)
        user_roles_table = UserRolesTable(self.connection, self.cursor)

        permissions_info = permissions_table.get_permission_by_path(self.permission_path)

        if not permissions_info:
            return False
        
        permissions_id = permissions_info['permission_id']

        # 获取用户角色id列表
        roles = user_roles_table.get_role_ids_by_user_id(self.user_id) # roles is a list of role_id

        # 获取角色权限列表
        permissions = role_permissions_table.get_role_permissions_by_role_ids(roles)

        return permissions_id in permissions


    